package cn.jlearn.proxy.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.jlearn.proxy.web.enums.TunnelOnlineEnum;
import cn.jlearn.proxy.web.enums.TunnelProtocolEnum;
import cn.jlearn.proxy.web.enums.TunnelTypeEnum;
import cn.jlearn.proxy.web.model.Tunnel;
import cn.jlearn.proxy.web.pojo.R;
import cn.jlearn.proxy.web.service.TunnelService;
import cn.jlearn.proxy.web.utils.EnumUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * @author qianyingchao
 * @version 1.0
 * @date 2021/7/15 10:50 上午
 */
@Controller
@RequestMapping(value = "/admin/tunnel")
public class TunnelController {

    @Resource
    private TunnelService tunnelService;

    @GetMapping(value = "/list.html")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("tunnel/list");
        String uid = StpUtil.getSession().getString("uid");
        mv.addObject("dataList", tunnelService.getTunnelList(uid));
        mv.addObject("protocolMap", EnumUtil.toMap(TunnelProtocolEnum.values()));
        mv.addObject("typeMap", EnumUtil.toMap(TunnelTypeEnum.values()));
        mv.addObject("onlineMap", EnumUtil.toMap(TunnelOnlineEnum.values()));
        return mv;
    }

    @GetMapping(value = "/add.html")
    public ModelAndView add() {
        return new ModelAndView("tunnel/add");
    }

    @ResponseBody
    @PostMapping(value = "/add/action")
    public R<?> addAction(@RequestBody JSONObject params) {
        String uid = StpUtil.getSession().getString("uid");
        return tunnelService.addTunnel(uid, params.getString("name"), params.getString("host"), params.getInteger("port"));
    }

    @GetMapping(value = "/{tunnelId}/edit.html")
    public ModelAndView edit(@PathVariable("tunnelId") BigInteger id) {
        ModelAndView mv = new ModelAndView("tunnel/edit");
        String uid = StpUtil.getSession().getString("uid");
        Tunnel tunnel = tunnelService.getTunnelById(id);
        if (tunnel != null && uid.equals(tunnel.getUid())) {
            mv.addObject("record", tunnel);
        }
        return mv;
    }

    @ResponseBody
    @PostMapping(value = "/edit/action")
    public R<?> editAction(@RequestBody JSONObject params) {
        if (params.getBigInteger("id") == null ||
                params.getString("name") == null ||
                params.getInteger("port") == null) {
            return R.fail("416", "参数不全");
        }
        String uid = StpUtil.getSession().getString("uid");
        return tunnelService.updateTunnel(params.getBigInteger("id"), uid,
                params.getString("name"), params.getString("host"), params.getInteger("port"));
    }

    @ResponseBody
    @PostMapping(value = "/change/token/action")
    public R<?> changeTokenAction(@RequestBody JSONObject params) {
        if (params.getBigInteger("id") == null) {
            return R.fail("416", "参数不全");
        }
        String uid = StpUtil.getSession().getString("uid");
        return tunnelService.changeToken(params.getBigInteger("id"), uid);
    }

}
